#include "mygrid.h"
#include<QDebug>
MyGrid::MyGrid(QString btnImg)
{
    QPixmap pix;
    bool ret=pix.load(btnImg);
    if(!ret){
        qDebug()<<QString("图片 %1 加载失败").arg(btnImg);
        return ;
    }
    this->setFixedSize(pix.width(),pix.height());
    //this->setStyleSheet("QPushButton{border:0px}");
    //不要设置透明了
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
    //初始化timer
    timer1=new QTimer(this);
    connect(timer1,&QTimer::timeout,[=](){
       QPixmap pix;
       QString str=QString(":/res/swirl%1").arg(this->min++);
       pix.load(str);
       this->setFixedSize(pix.width(),pix.height());
       this->setStyleSheet("QPushButton{border:0px}");
       this->setIcon(pix);//相当于改变图标
       this->setIconSize(QSize(pix.width(),pix.height()));
       //翻完了重置为1
       if(this->min>this->max){
           this->min=1;
           //timer1->stop();//计时器不要停下来
       }
    });
}
void MyGrid::changeFlag(){
    timer1->start(30);
    this->isAnimation=true;
};
void MyGrid::stopFlag(int stopto){//必须说明之后的数字是多少
    this->GridNum=stopto;//不要忘记改值
    if(stopto==-1){//相当于还原操作
        timer1->stop();
        QPixmap pix;//改变图标
        QString str=QString(":/res/Num%0.png").arg(0);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        //this->setStyleSheet("QPushButton{border:0px}");
        //不要设置透明了
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    else{
        timer1->stop();
        QPixmap pix;//改变图标
        QString str=QString(":/res/Num%1.png").arg(stopto);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        //this->setStyleSheet("QPushButton{border:0px}");
        //不要设置透明了
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
};

void MyGrid::mousePressEvent(QMouseEvent *e){
    if(this->iswin){
        return ;//可以拦截操作
    }
    else {
        QPushButton::mousePressEvent(e);//不写这句全部拦截
    }
}
